import type { BaseBridgeMetadata, BaseCommissioningInfo } from '../bridgeTypes.js'

/**
 * Shared Matter Types
 *
 * These types are used by both the homebridge core and the UI
 * to ensure consistency across the Matter implementation.
 *
 * Matter-specific interfaces extend the base bridge types for
 * consistency with HAP child bridges.
 */

/**
 * Matter bridge status states
 */
export enum MatterBridgeStatus {
  /**
   * When the Matter bridge is loading or restarting
   */
  PENDING = 'pending',

  /**
   * The Matter bridge is online and ready for commissioning
   */
  OK = 'ok',

  /**
   * The bridge is shutting down or stopped
   */
  DOWN = 'down',
}

/**
 * Metadata for a Matter bridge instance
 *
 * Extends the base bridge metadata with Matter-specific properties.
 * This ensures consistency with HAP child bridge metadata.
 */
export interface MatterBridgeMetadata extends Omit<BaseBridgeMetadata, 'type' | 'status'> {
  /** Bridge type identifier (always 'matter') */
  type: 'matter'
  /** Current operational status */
  status: MatterBridgeStatus
  /** QR code payload for commissioning */
  qrCode?: string
  /** Manual pairing code for commissioning */
  manualPairingCode?: string
  /** Device serial number */
  serialNumber?: string
  /** Number of devices exposed by this bridge */
  deviceCount: number
  /** Whether the bridge has been commissioned */
  commissioned?: boolean
}

/**
 * Matter commissioning information
 *
 * Extends the base commissioning info with Matter-specific properties.
 * This ensures consistency with HAP pairing information.
 */
export interface MatterCommissioningInfo extends Omit<BaseCommissioningInfo, 'type' | 'commissioned'> {
  /** Bridge type (always 'matter') */
  type?: 'matter'
  /** QR code payload for commissioning */
  qrCode?: string
  /** Manual pairing code for commissioning */
  manualPairingCode?: string
  /** Whether the device is commissioned */
  commissioned: boolean
}

/**
 * Matter accessory information
 */
export interface MatterAccessoryInfo {
  /** Unique identifier */
  uuid: string
  /** Display name */
  displayName: string
  /** HAP category */
  category: number
  /** Matter device information */
  matterInfo?: {
    /** Whether this is a bridged device */
    bridged: boolean
    /** Child bridge identifier if bridged */
    childBridge?: string
    /** Matter device type */
    deviceType?: string
  }
  /** HAP services */
  services?: Array<{
    type: string
    subtype?: string
    displayName?: string
    characteristics: Array<{
      type: string
      value: any
      props: any
    }>
  }>
}

/**
 * Matter server configuration
 * Used internally by MatterServer class
 */
export interface MatterServerConfig {
  /** Server port */
  port?: number
  /** Unique identifier (REQUIRED - must be unique for each Matter bridge instance) */
  uniqueId: string
  /** Storage path */
  storagePath?: string
  /** Manufacturer name (inherited from bridge config) */
  manufacturer?: string
  /** Model name (inherited from bridge config) */
  model?: string
  /** Firmware revision (inherited from bridge config) */
  firmwareRevision?: string
  /** Serial number (inherited from bridge config) */
  serialNumber?: string
  /** Enable debug mode for verbose logging */
  debugModeEnabled?: boolean
}

/**
 * Matter accessories collection
 */
export interface MatterAccessoriesResponse {
  /** Child bridge accessories indexed by bridge ID */
  children: { [bridgeId: string]: MatterAccessoryInfo[] }
}

/**
 * IPC message types for Matter child bridges
 */
export enum ChildMatterMessageType {
  /** Sent from child when ready to accept config */
  READY = 'ready',
  /** Sent to child with configuration */
  LOAD = 'load',
  /** Sent from child when loaded */
  LOADED = 'loaded',
  /** Sent to child to start the Matter bridge */
  START = 'start',
  /** Sent from child when Matter bridge is online */
  ONLINE = 'online',
  /** Sent to/from child to add an accessory */
  ADD_ACCESSORY = 'addAccessory',
  /** Sent to/from child to remove an accessory */
  REMOVE_ACCESSORY = 'removeAccessory',
  /** Sent from child with status updates */
  STATUS_UPDATE = 'statusUpdate',
  /** Sent from child when an error occurs */
  ERROR = 'error',
  /** Sent to child to shut down */
  SHUTDOWN = 'shutdown',
}
